/**
 * @param {string[]} words
 * @return {string}
 */
export var longestWord = function (words) {
  // 按长度降序，按字典序降序
  words.sort((a, b) => {
    if (a.length != b.length) return a.length - b.length
    return a.localeCompare(b)
  })
  if (words[0].length != 1) return ''
  let ans = ''
  // 记录合要求的单词
  const set = new Set([''])

  for (let i = 0; i < words.length; i++) {
    const word = words[i]
    if (set.has(word.slice(0, -1))) {
      set.add(word)
      if (word.length > ans.length) ans = word
    }
  }
  return ans
}
